/**swiper说明
 *1.支持图片比例不同
 *2.支持视频和图片共存
 *3.传值
 * 3.1图片格式imgslist:[
 *   {"imgurl":"http://cx002exrds.7mx7.com/images/5/2020/07/ROEBs7TO7tqWcw0cz06aTeeqTosss5.jpg", status: "1"},
 *   {"imgurl":"http://cx002exrds.7mx7.com/images/5/2020/07/OZ4qMFYOQo4MlFQuLEZm44VZlQluQ6.jpg", status: "1"},       
 * ]
 * 3.2视频封面图videoimg：'http://cx002exrds.7mx7.com/images/5/2020/07/OZ4qMFYOQo4MlFQuLEZm44VZlQluQ6.jpg'
 * 3.3视频链接videourl：'https://ips.ifeng.com/video19.ifeng.com/video09/2020/10/23/p37487196-102-009-083137.mp4?reqtype=tsl&vid=e01c9126-bdb7-44ec-ae23-2484d98f295b&uid=X0kcCC&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=&cat=&ptype=&platform=pc&sourceType=h5&dt=1603437382054&gid=eZUogXVUUMmm&sign=27bcf084db81127b6eaf8b5eb713fb25&tm=1603437382054'
 * 3.4.样式说明
 * customStyle: {
 *     type: 2, //类型1 视频图片左右切换 类型2带小图
 *     status: true,//是否开启轮播图
 *     color: "#3DB963"//类型1：按钮显示颜色 类型二：小图选中色
 *    }
 */
Component({
  /**
   * 组件的一些选项
   */
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // swiper样式
    customStyle: {
      type: Object,
      value:{
        mode: 1,
        status: true,
        color: "#3DB963"
      }
    },
    // 图片列表
    imgslist: {
      type: Array,
    },
    // 视频封面图
    videoimg: {
      type: String,
    },
    // 视频链接
    videourl: {
      type: String,
      observer: function (newVal, oldVal) {
        if (newVal) {
            this.setData({
              swiperCurrent: -1
            })
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: 1, //默认1视频 2图片,
    play: -1, //视频状态1播放 -1未播放
    firstCurrent: 0, //类型一图片切换第几个
    swiperCurrent: 0, // 轮播图
    current: 0, //第几个大图
    smallblock: true, //小图部分状态开启
    // customStyle: {
    //   type: 2,
    //   status: true,
    //   play: "https://cx002ex.7mx7.com/addons/xc_lshop/eq/images/play.png",
    //   color: "#3DB963"
    // }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    navtolive(e){
      var item=e.currentTarget.dataset.item;
      if(item.cid==1){
        wx.navigateTo({
          url: item.url,
        })
      }else if(item.cid==6){
       var roomId = item.roomid // 填写具体的房间号，可通过下面【获取直播房间列表】 API 获取
       var customParams = encodeURIComponent(JSON.stringify({
         roomid: roomId
       })) // 开发者在直播间页面路径上携带自定义参数（如示例中的path和pid参数），后续可以在分享卡片链接和跳转至商详页时获取，详见【获取自定义参数】、【直播间到商详页面携带参数】章节（上限600个字符，超过部分会被截断）
       wx.navigateTo({
         url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
       })
      }
   },
    touchStart: function (e) {
      if (e.touches.length == 1) {
        this.setData({
          startX: e.touches[0].clientX
        });
      }
    },
    touchMove: function (e) {
      if (e.touches.length == 1) {
        var moveX = e.touches[0].clientX;
        var towards = this.data.startX - moveX;
        this.setData({
          towards: towards
        })
      }
    },
    touchEnd: function (e) {
      let that = this
      if (that.data.towards != '') {
        if (that.data.towards < 0) { //向右
          console.log('向右')
          console.log(that.data.towards)
        } else if (that.data.towards > 0) { //向左
          console.log('向左')
          console.log(that.data.towards)

        }
        var update = {};
        if (that.data.customStyle.type == 1) {
          if (that.data.show == 1 && that.data.towards > 0) {
            update['show'] = 2;
          } else if (that.data.show == 2 && that.data.towards < 0 && that.data.firstCurrent == 0) {
            update['show'] = 1;
            console.log(that.data.show == 2)
            console.log(that.data.towards < 0)
            console.log(that.data.firstCurrent == 0)

          }
        }else if (that.data.customStyle.type == 2 && that.data.towards > 0){
          if (this.data.videourl && this.data.videoimg) {
            this.setData({
              current:1,
              swiperCurrent:0,
              play: -1,
              smallblock: true
            })
          } else {
            this.setData({
              swiperCurrent: 0
            })
          }
        }
        that.setData(update)
      }
    },
    firstChange: function (e) {
      this.setData({
        firstCurrent: e.detail.current
      })  
    },
    // 点击播放
    start: function (e) {
      this.setData({
        play: 1
      })
      if (this.data.customStyle.type == 1) {
        var videoPlayer = wx.createVideoContext('myfirstVideo', this);
        videoPlayer.play();
      } else {
        var videoPlayer = wx.createVideoContext('myVideo', this);
        videoPlayer.play();
        this.setData({
          smallblock: false
        })
      }


      // var videoPlayer = wx.createVideoContext('myVideo', this);
      // var lastRunTime = Date.now();
      // var currentTime = Date.now();
      // var protectTime = 100; //设置保护性延时 单位毫秒，不要小于50 建议100以上
      // if ((currentTime - lastRunTime) < protectTime) {
      //   return; //两次执行太过频繁，直接退出
      // }
      // if (videoPlayer.paused) {
      //   videoPlayer.play();
      // } else {
      //   videoPlayer.pause();
      // }
      // lastRunTime = Date.now();


    },
    videoended: function () {
      this.setData({
        play: -1,
        smallblock: true
      })
    },
    videopause:function(e){
      this.setData({
        play: -1,
        smallblock: true
      })
    },
 
    // 视频图片切换
    changebtn: function (e) {
      var dataset = e.currentTarget.dataset;
      this.setData({
        show: dataset.value
      })
    },
    //当前图片高度
    imgH: function (e) {
    
      this.setData({
        swiperheight: e.detail.height / e.detail.width * 100
      })
      // this.data.imgslist[e.currentTarget.dataset.index]["height"] = e.detail.height / e.detail.width * 100;
      // if ((e.currentTarget.dataset.index == this.data.swiperCurrent) && !this.data.coverurl) {
      //   this.setData({
      //     swiperheight: this.data.imgslist[e.currentTarget.dataset.index]["height"]
      //   })
      // }
    },
    //当前视频高度
    videoH: function (e) {
      this.setData({
        swiperheight: e.detail.height / e.detail.width * 100
      })
    },
    // 轮播图选择属性
    choose_attr(e) {
      if (this.data.videourl && this.data.videoimg) {
        this.setData({
          current: parseInt(e.currentTarget.dataset.index) + 1
        })
      } else {
        this.setData({
          current: e.currentTarget.dataset.index
        })
      }
      this.setData({
        swiperCurrent: e.currentTarget.dataset.index
      })
    },
    swiperchange: function (e) {
      var that = this;
      if (this.data.videourl && this.data.videoimg) {
        this.setData({
          swiperCurrent: parseInt(e.detail.current) - 1
        })
      } else {
        this.setData({
          swiperCurrent: e.detail.current
        })
      }
    }
  }
})